package com.eightbears.beboy.base.view.popupwindow;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.entity.ClassifyEntity;
import com.eightbears.beboy.entity.LocationEntity;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.FindDistrictSortAdapter;
import com.eightbears.beboy.adapter.LocalMenuAdapter;
import com.eightbears.beboy.adapter.LocationItemAdapter;
import com.eightbears.beboy.base.cache.DistrictCache;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.view.basepopu.BasePopupWindow;
import com.eightbears.greendao.District;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bao on 2017/5/3.
 */
public class LocationSortPopup extends BasePopupWindow {

    private View popupView;
    private RecyclerView rl_classify, rl_menu;

    private LocationItemAdapter mLocationItemAdapter;
    private LocalMenuAdapter mLocalMenuAdapter;
    private FindDistrictSortAdapter mFindDistrictSortAdapter;

    private OnClickItemListener mOnClicktemListener;
    private OnMenuClickListener mOnMenuClickListener;
    private OnDistrictClickListener mOnDistrictClickListener;

    private List<ClassifyEntity> menuList, locationList;
    private List<District> mDistricts;
    private ClassifyEntity mClassifyEntity, mMenuEntity;
    private District mDistrict;


    public LocationSortPopup(Activity context) {
        super(context);
        bindEvent();
        initAdapter();
    }

    @Override
    public Animation getAnimation() {
        return getDefaultAlphaAnimation();
    }

    @Override
    public AnimationSet getAnimationSet() {
        return null;
    }

    @Override
    public View getInputView() {
        return null;
    }

    @Override
    public View getDismissView() {
        return popupView.findViewById(R.id.click_to_dismiss);
    }

    @Override
    public View getPopupView() {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.view_find_location_popup, null);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    private void bindEvent() {
        if (popupView != null) {
            rl_classify = (RecyclerView) popupView.findViewById(R.id.rl_classify);
            rl_menu = (RecyclerView) popupView.findViewById(R.id.rl_menu);
        }
    }

    private void initAdapter() {
        initData();
        mLocalMenuAdapter = new LocalMenuAdapter();
        rl_menu.setLayoutManager(new LinearLayoutManager(mContext));
        mLocalMenuAdapter.setNewData(menuList);
        rl_menu.setAdapter(mLocalMenuAdapter);
        mLocalMenuAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mMenuEntity = (ClassifyEntity) adapter.getData().get(position);
                mOnMenuClickListener.onMenuClick(mMenuEntity);
                selectMenu(mMenuEntity);
                if (mMenuEntity.getId().equals("1")) {
                    initLocation();
                } else {
                    initDistrictAdapter();
                }

            }
        });
        initLocation();
    }

    private void initLocation() {
        mLocationItemAdapter = new LocationItemAdapter();
        rl_classify.setLayoutManager(new LinearLayoutManager(mContext));
        mLocationItemAdapter.setNewData(locationList);
        rl_classify.setAdapter(mLocationItemAdapter);
        mLocationItemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mClassifyEntity = (ClassifyEntity) adapter.getData().get(position);
                mOnClicktemListener.onClickItem(mClassifyEntity);
                dismiss();
            }
        });
    }

    private void initDistrictAdapter() {
        mFindDistrictSortAdapter = new FindDistrictSortAdapter();
        rl_classify.setLayoutManager(new LinearLayoutManager(mContext));
        mFindDistrictSortAdapter.setNewData(mDistricts);
        rl_classify.setAdapter(mFindDistrictSortAdapter);
        mFindDistrictSortAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mDistrict = (District) adapter.getData().get(position);
                mOnDistrictClickListener.onDistrictClick(mDistrict);
                dismiss();
            }
        });
    }

    private void initData() {
        menuList = new ArrayList<>();
        ClassifyEntity all = new ClassifyEntity();
        all.setClassifyName("附近");
        all.setId("1");
        menuList.add(all);
        ClassifyEntity sangna = new ClassifyEntity();
        sangna.setClassifyName("区域");
        sangna.setId("2");
        menuList.add(sangna);
        SPUtil.putLocationMenuClass(menuList.get(0));

        locationList = new ArrayList<>();
        ClassifyEntity classifyEntity1 = new ClassifyEntity();
        classifyEntity1.setId("0");
        classifyEntity1.setClassifyName("附近");
        locationList.add(classifyEntity1);
        ClassifyEntity classifyEntity2 = new ClassifyEntity();
        classifyEntity2.setId("1");
        classifyEntity2.setClassifyName("1000米");
        locationList.add(classifyEntity2);
        ClassifyEntity classifyEntity3 = new ClassifyEntity();
        classifyEntity3.setId("3");
        classifyEntity3.setClassifyName("3000米");
        locationList.add(classifyEntity3);
        ClassifyEntity classifyEntity4 = new ClassifyEntity();
        classifyEntity4.setId("5");
        classifyEntity4.setClassifyName("5000米");
        locationList.add(classifyEntity4);
        ClassifyEntity classifyEntity5 = new ClassifyEntity();
        classifyEntity5.setId("10");
        classifyEntity5.setClassifyName("10000米");
        locationList.add(classifyEntity5);
        ClassifyEntity classifyEntity6 = new ClassifyEntity();
        classifyEntity6.setId("15");
        classifyEntity6.setClassifyName("15000米");
        locationList.add(classifyEntity6);
        SPUtil.putLocationContentClass(locationList.get(0));
        getDistricts();
    }

    private void getDistricts() {
        LocationEntity locationEntity = SPUtil.getUserLocation();
        mDistricts = DistrictCache.getInstance(mContext).getDistircts(locationEntity.getCityId(), DistrictCache.CODE_DISTRICT);
        SPUtil.putFindDistrictClass(null);
    }

    public interface OnMenuClickListener {
        void onMenuClick(ClassifyEntity menuEntity);
    }
    public interface OnDistrictClickListener {
        void onDistrictClick(District district);
    }

    public interface OnClickItemListener {
        void onClickItem(ClassifyEntity classifyEntity);
    }

    public void setOnDistrictClickListener(OnDistrictClickListener onDistrictClickListener) {
        this.mOnDistrictClickListener = onDistrictClickListener;
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.mOnMenuClickListener = onMenuClickListener;
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.mOnClicktemListener = onClickItemListener;
    }

    /**
     * 选择距离
     */
    public void selectItem(ClassifyEntity classifyEntity) {
        SPUtil.putLocationContentClass(classifyEntity);
        mLocationItemAdapter.selAutoSort(classifyEntity);
    }

    /**
     * 选择菜单
     */
    public void selectMenu(ClassifyEntity classifyEntity) {
        SPUtil.putLocationMenuClass(classifyEntity);
        if(null != mLocalMenuAdapter)
            mLocalMenuAdapter.selMenu(classifyEntity);
    }
    /**
     * 选择菜单
     */
    public void selectDistrict(District district) {
        SPUtil.putFindDistrictClass(district);
        if(null != mFindDistrictSortAdapter)
            mFindDistrictSortAdapter.selDistrict(district);
    }
}
